package com.zhaojiu.example.service.beetl.function;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author huangshb
 * @date 2024/8/5
 */
public class PathToMethodName implements Function {
    @Override
    public Object call(Object[] paras, Context ctx) {
        if (paras == null || paras.length == 0) {
            return "UnKnown";
        }
        Object o = paras[0];
        if (o != null) {
            String path = o.toString();
            path = path.replace("\\","");
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            String[] strings = path.split("/");
            if (strings.length == 1) {
                return strings[0];
            } else {
                return strings[0] + Arrays.stream(strings)
                        .skip(1)
                        .filter(s -> !StringUtils.isEmpty(s))
                        .map(s -> s.substring(0, 1).toUpperCase() + (s.length() > 1 ? s.substring(1): ""))
                        .collect(Collectors.joining());
            }

        }

        return "";
    }
}
